﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using DataModel;

namespace Mvc4
{
    public class SubdomainConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            
            if (routeDirection == RouteDirection.IncomingRequest && parameterName.ToLowerInvariant() == "site")
            {
                var siteName = values[parameterName] as string;
                if (siteName == null)
                {
                    return false;
                }
                else
                {
                    siteName = siteName.ToLower();
                    if (siteName == "account" || siteName == "home")
                    {
                        return false;
                    }
                    //Provisorio
                    ModelContainer db = new ModelContainer();
                    IEnumerable<SiteSet> sites = db.SiteSet.ToList();
                    foreach (var site in sites)
                    {    
                        if (site.Name.ToLower() == siteName)
                        {
                            values.Add("siteId",site.id);
                            values.Add("siteName",site.Name);
                            return true;
                        }
                    }
                }
            }
            
            return false;
        }

    }

    public class ControllerConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {

            if (routeDirection == RouteDirection.IncomingRequest && parameterName.ToLowerInvariant() == "controller")
            {
                var controllerName = values[parameterName] as string;
                if (controllerName == null)
                {
                    return true;
                }
                else
                {
                    controllerName = controllerName.ToLower();
                    if (controllerName == "site" || controllerName == "good" || controllerName == "signalr")
                    {
                        return false;
                    }
                }
            }

            return true;
        }

    }

    public class ControllerSiteConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {

            if (routeDirection == RouteDirection.IncomingRequest && parameterName.ToLowerInvariant() == "controller")
            {
                var controllerName = values[parameterName] as string;
                if (controllerName == null)
                {
                    return true;
                }
                else
                {
                    controllerName = controllerName.ToLower();
                    if (controllerName == "site")
                    {
                        return false;
                    }
                }
            }

            return true;
        }

    }

}